/**
 * @description: 全员营销活动|登录注册页相关
 *
 * @author Yanghc
 * @date 2018年7月11日 下午09:10:52
 * @version 1.0.0.1
 */
var path=$("#pathId").val();

 //注册 --发送验证码
 $('.regSendCode').click(function(){
	 var dataf=$(this).attr("data-flag");
	 if(dataf=="0"){
		 return;
	 }
	 
	 var flag=true;
	 var regPhone=$("#phone").val();
	 if(regPhone==null||regPhone==""){
		 showRegRemind("手机号不能为空！");
 		 flag=false;
 		 return;
 	}else if(!/^1[34578]\d{9}$/.test(regPhone)){
 		showRegRemind("手机号格式不正确！");
 		flag=false;
 		return;
 	}
	 
	//变灰不可点击
	$(this).css('color','#efefef');
	$(this).attr("data-flag","0");
	//发送短信
	if(flag){
		var $this = $(this),
        _this = this,
        i = 60;
		var url = path+"/aem/client/ajaxSendSmsIdentiCode";
  		$.post(url,{
			"phone" :regPhone,
			"cid":cid,
			"codeType":"register"
		   },function(data) {
  			if (data.status =="200") {
  				 clearInterval(repeat);
  				 var repeat = setInterval(function(){
  		            i--;
  		            if(i == 0){
  		                $this.html('获取验证码');
  		                $this.css('color','#ff892f');
  		                $this.attr("data-flag","1");
  		                clearInterval(repeat);
  		            }else{
  		                $this.html(i+'s后重新获取');
  		                $this.attr("data-flag","0")
  		            }
  		        },1000);
  			}else{
  				$this.html('获取验证码');
	            $this.css('color','#ff892f');
	            $this.attr("data-flag","1");
  				showRegRemind(data.message);
        	}
  		});
	}
 });
 
//登录--发送验证码
 $('.loginSendCode').click(function(){
	 var dataflag=$(this).attr("data-flagone");
	 if(dataflag=="0"){
		 return;
	 }
	 var flag=true;
	 var logPhone=$("#loginPhone").val();
	 if(logPhone==null||logPhone==""){
		 flag=false;
		 showloginRemind("手机号不能为空！");
		 return;
 	}else if(!/^1[34578]\d{9}$/.test(logPhone)){
 		 flag=false;
		 showloginRemind("手机号格式不正确！");
		 return;
 	}
	 
	//变灰不可点击
	$(this).css('color','#efefef');
	$(this).attr("data-flagone","0");
	//发送短信
	if(flag){
		var $this = $(this),
        _this = this,
        i = 60;
		var url = path+"/aem/client/ajaxSendSmsIdentiCode";
  		$.post(url,{
			"phone" :logPhone,
			"cid":cid,
			"codeType":"login"
		   },function(data) {
  			if (data.status =="200") {
  				 clearInterval(repeat);
  				 var repeat = setInterval(function(){
  		            i--;
  		            if(i == 0){
  		                $this.html('获取验证码');
  		                $this.css('color','#ff892f');
  		                $this.attr("data-flagone","1");
  		                clearInterval(repeat);
  		            }else{
  		                $this.html(i+'s后重新获取');
  		                $this.attr("data-flagone","0");
  		            }
  		        },1000);
  			}else{
  				 $this.html('获取验证码');
	             $this.css('color','#ff892f');
	             $this.attr("data-flagone","1");
  				showloginRemind(data.message);
        	}
  		});
	}
 });
 
 //注册提交
 $('.regBtn').click(function(){
	  var flag=true;
	  var userName=$("#name").val();
	  var mobile=$("#phone").val();
	  var smsCode=$("#smsRegCode").val();
	  var deptCode=$("#deptCode").val();
	  var email=$("#email").val();
	  var idCard=$("#idCard").val();
	  var birthday = $('#birthday').val();
	  var birthdayone;
	  var sex=$('input:radio[name="sex"]:checked').val();
	  var detailAddress = $('#detailAddress').val();
	  var address = $('#address').val();
	  
	  //姓名
	  if(!StringEmpty(userName)){
		  flag=false;
		  showRegRemind("请输入姓名！");
		  return;
	  }
	  //手机
	  if(!StringEmpty(mobile)){
		  flag=false;
		  showRegRemind("请输入手机！");
		  return;
	  }else if(!/^1[34578]\d{9}$/.test(mobile)){
		  showRegRemind("手机号格式不正确！");
	 	  flag=false;
	 	  return;
	  }
	  //身份证校验
	  var idrequ=$("#idCard").attr("cardRequired");
	  if(idrequ=="1"){//必填
			  if(!StringEmpty(idCard)){
				  flag=false;
				  showRegRemind("请输入身份证号");
				  return;
			  }else{
				if(!checkIDCard(idCard)){
					 flag=false;
					 showRegRemind("请输入正确的身份证号");
					 return;
				}  
			  }
		}else{//非必填 如果填写，校验身份证格式
			  if(StringEmpty(idCard) && !checkIDCard(idCard)){
				  flag=false;
					 showRegRemind("请输入正确的身份证号");
					 return;
			  }
		}
	  
	  
	  //出生日期校验
	  var birRequ=$('#birthday').attr("birRequired");
	  if(birRequ=="1"){//必填
		  if(!StringEmpty(birthday)){
			  flag=false;
			  showRegRemind("请输入出生日期");
			  return;
		  }
	  }
	  
	  
	  //邮箱校验
	  var emailRequ=$("#email").attr("emailRequired");
	  if(emailRequ=="1"){//必填
		  if(!StringEmpty(email)){
			  flag=false;
			  showRegRemind("请输入邮箱");
			  return;
		  }else if(!memail(email)){
			  flag=false;
			  showRegRemind("请输入正确的邮箱");
			  return;
		  }
	  }else{//非必填  如果填写邮箱，校验邮箱格式
		  if(StringEmpty(email) && !memail(email)){
			  flag=false;
			  showRegRemind("请输入正确的邮箱");
			  return;
		  } 
	  }
	  
	  //性别校验
	  var sexRequ=$("#sex").attr("sexRequired");
	  if(sexRequ=="1"){//必填
		  if(!StringEmpty(sex)){
			  flag=false;
			  showRegRemind("请选择性别");
			  return;
		  }
	  }
	  
	  //地址校验
	  var areaRequ=$("#address").attr("areaRequired");
	  if(areaRequ=="1"){//必填
		  if(!StringEmpty(address)){
			  flag=false;
			  showRegRemind("请选择地址");
			  return;
		  }
	  }
	  
	  //详细地址校验
	  var deRequ=$('#detailAddress').attr("deRequired");
	  if(deRequ=="1"){//必填
		  if(!StringEmpty(detailAddress)){
			  flag=false;
			  showRegRemind("请填写详细地址");
			  return;
		  }
	  }
	  
	  //短信验证码
	  if(!StringEmpty(smsCode)){
		  flag=false;
		  showRegRemind("请输入验证码！");
		  return;
	  }
	  //部门邀请码
	  if(!StringEmpty(deptCode)){
		  flag=false;
		  showRegRemind("请输入部门邀请码！");
		  return;
	  }
	  
	  //地址省市县处理
	  var provinceId;
	  var cityId;
	  var areaId;
	  if(address){
		    var arr = address.split(' ');
	        provinceId=arr[0];
	        cityId=arr[1];
	        areaId=arr[2];
	  }
	  
	  //出生日期转换
	  if(StringEmpty(birthday)){
		  birthdayone=new Date(birthday);
	  }
	  if(flag){
		 var addUrl=path+"/aem/client/ajaxAddRegistStaffInfo";
 		 $.ajax({
 	 			type:"post",
 	  			url:addUrl,
 	  			async: false,
 	  			data:{
 	            	"name":userName,
 	            	"phone":mobile,
 	            	"departmentCode":deptCode,
 	            	"verificationCode":smsCode,
 	            	"headImg":headImgUrl,
 	            	"openId":openId,
 	            	"cid":cid,
 	            	"email":email,
 	            	"idCard":idCard,
 	            	"sex":sex,
 	            	"birthday":birthdayone,
 	            	"provinceId":provinceId,
 	                "cityId":cityId,
 	                "areaId":areaId,
 	                "detailAddress":detailAddress,
 	            	},
 	            success:function(data){
 	            	if(data.status =="200"){
 	            		$("#name").val("");
 	            		$("#phone").val("");
 	            	    $("#smsRegCode").val("");
 	            		$("#deptCode").val("");
 	            		$(".regSendCode").html("获取验证码");
 	            		$(".regSendCode").css('color','#ff892f');
 	            		showRegRemind("注册成功");
 	            		window.location.href =path+"/aem/client/staffHome?cid="+cid;
 	            	}else if(data.status=="201"){//验证码失效
 	            		showRegRemind(data.message);
 	            	}else if(data.status=="400"){//验证码输入有误
 	            		showRegRemind(data.message);
 	            	}else{
 	         		    showRegRemind(data.message);
 	            	}
 	            }
 	 	}); 
	  }
	  
 });
 
 //登录提交
 $(".loginBtn").click(function(){
	  var flag=true;
	  var logmobile=$("#loginPhone").val();
	  var logSmCode=$("#smsLogCode").val();
	  
	  //手机
	  if(!StringEmpty(logmobile)){
		  flag=false;
		  showloginRemind("请输入手机！");
		  return;
	  }else if(!/^1[34578]\d{9}$/.test(logmobile)){
		  showRegRemind("手机号格式不正确！");
	 	  flag=false;
	 	  return;
	  }
	  //短信验证码
	  if(!StringEmpty(logSmCode)){
		  flag=false;
		  showloginRemind("请输入验证码！");
		  return;
	  }
	  if(flag){
			 var addUrl=path+"/aem/client/ajaxStaffInfoLogin";
	 		 $.ajax({
	 	 			type:"post",
	 	  			url:addUrl,
	 	  			async: false,
	 	  			data:{
	 	            	"phone":logmobile,
	 	            	"verificationCode":logSmCode,
	 	            	"headImg":headImgUrl,
	 	            	"openId":openId,
	 	            	"cid":cid
	 	            	},
	 	            success:function(data){
	 	            	if(data.status =="200"){
	 	            		$("#loginPhone").val("");
	 	            		$("#smsLogCode").val("");
	 	            		$(".loginSendCode").html("获取验证码");
	 	            		$(".loginSendCode").css('color','#ff892f');
	 	            		window.location.href =path+"/aem/client/staffHome?cid="+cid;
	 	            	}else if(data.status=="201"){//验证码失效
	 	            		showloginRemind(data.message);
	 	            	}else if(data.status=="400"){//验证码输入有误
	 	            		showloginRemind(data.message);
	 	            	}else{
	 	            		showloginRemind(data.message);
	 	            	}
	 	            }
	 	 	}); 
		  }
 });
 
 //注册提示
 function showRegRemind(mes){
	 $("#regRemind").show().delay(5000).fadeOut();
     $("#viewregRem").html(mes);
 }
 
 //登录提示
 function showloginRemind(message){
	 $("#logRemind").show().delay(5000).fadeOut();
	 $("#logViewRem").html(message);
 }
 
 function StringEmpty(content) {
		if (content == undefined || content == "" || content == null || content=="undefined" || content =="null") {
			return false;
		} else {
			return true;
		}
}
 
 //判断邮箱
function memail(eemail){
	 var regEmail = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
     var result = regEmail.test(eemail);
     if(!result){
         return false;
     }else{
         return true;
     }
 }

//判断身份证
function checkIDCard(idCard){
	var idCardReg =  /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
	var result=idCardReg.test(idCard);
	 if(!result){
         return false;
     }else{
         return true;
     }
}